package org.ocean.online.web.workspace;

import java.io.File;

import net.sf.json.JSONArray;

import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.wicket.markup.MarkupType;
import org.ocean.online.utils.CommonUtil;
import org.ocean.online.vo.TreeNode;
import org.ocean.online.wicket.NeedLoginPage;

public class PackageExplorer extends NeedLoginPage{

	private static final long serialVersionUID = -7576534588455109116L;

	@Override
	public MarkupType getMarkupType() {
		return new MarkupType("json", "application/json");
	}

	@Override
	public void renderPage() {
		String path = this.getParamValue("path");
//		path = path.replace(Constant.PackageSeparator, File.separator);
		path=this.getWorkspace()+path;
		System.out.println("package path :" +path);
		File ff = new File(path);
		JSONArray arr = new JSONArray();
		for(File file: ff.listFiles()){
			TreeNode node= new TreeNode();
			node.setText(file.getName());
			node.getAttributes().put("isNew", false);
			if(file.isDirectory()){
				node.getAttributes().put("type", "package");
				node.setState("closed");
			}else if(file.isFile()){
				node.getAttributes().put("type", "file");
				String ext = CommonUtil.getExtOfFile(file.getPath());
				if("jpg,jpeg,bmp,raw,png,gif".contains(ext.toLowerCase())){
					ext="image";
				}
				node.setIconCls("icon-"+ext.toLowerCase());
			}
			
			arr.add(node);
			
		}
		this.getResponse().write(arr.toString().getBytes());
	}
	
	class ExplorerFilter extends TrueFileFilter{

		@Override
		public boolean accept(File dir, String name) {
			return true;
		}

		@Override
		public boolean accept(File file) {
			return true;
		}
		
	}
}
